<?php

/*
 * hlavní funkce, která obsluhuje /mailcontent/content - vypisuje veškeré příchozí maily
 * a stará se o přesměrování na stránky jednotlivé výčty typů, když se některý maže nebo upravuje
 * $p1, $p2, $p3 - parametry předané callbackem (z webové adresy ///)
 * @ vrací celou stránku výpisu
*/
function mailcontent_content($p1 = '', $p2 = '', $p3 = '') {
    global $user;
    if (user_access('content user')) {
        $text = "<hr><br><br>";
        if ($p1 != '') {
            if ($p2 == '') {
                drupal_goto("mailcontent/content");
            }
            if ($p2 == 'idm' || $p2 == 'xidm' || $p2 == 'from_address' || $p2 == 'xfrom_address' || $p2 == 'type_content' || $p2 == 'xtype_content' || $p2 == 'received' || $p2 == 'xreceived' || $p2 == 'duid' || $p2 == 'xduid' || $p2 == 'state' || $p2 == 'xstate' || $p2 == 'dateexp' || $p2 == 'xdateexp' || $p2 == 'datestart' || $p2 == 'xdatestart') {
                if (substr($p2, 0, 1) != 'x') {
                    $order = $p2;
                    $sort = '';
                } else {
                    $order = substr($p2, 1);
                    $sort = 'DESC';
                }
                mailcontent_set_filter($p1, $order, $sort);
                drupal_goto("mailcontent/content/".$p1."/all");
            }

            if ($p2 == 'delmail') {
                return mailcontent_delmail($p3, $p1);
            }

            if ($p2 == 'delnode') {
                mailcontent_delnode_for_mail($p3);
                if ($p1 == 'content') {
                    drupal_goto("mailcontent/content");
                } else {
                    drupal_goto("mailcontent/content/".$p1."/all");
                }
            }

            if ($p2 == 'publish') {
                mailcontent_publish($p3);
                if ($p1 == 'content') {
                    drupal_goto("mailcontent/content");
                } else {
                    drupal_goto("mailcontent/content/".$p1."/all");
                }
            }

            if ($p2 == 'gotonode') {
                drupal_goto("node/".$p3);
            }


            if ($p1 == 'article') {
                if (substr($p2, 0, 1) == 'X') {
                    drupal_goto("mailcontent/view/".substr($p2,1));
                } else {
                    $GLOBALS['mailcontent_ret'] = 'article';
                    mailcontent_publish($p2);

                }
            }

            if ($p1 == 'inquiry') {
                if (substr($p2, 0, 1) == 'X') {
                    drupal_goto("mailcontent/view/".substr($p2,1));
                } else {
                    drupal_goto("mailcontent/inquiry_view/".$p2);
                }
            }
            if ($p1 == 'schedule' || $p1 == 'task') {
                if (substr($p2, 0, 1) == 'X') {
                    drupal_goto("mailcontent/view/".substr($p2,1));
                } else {
                    mailcontent_work_on($p2);
                }
            }

            if ($p1 == 'content') {
                drupal_goto('mailcontent/content');
            } else {
                drupal_goto("mailcontent/content/".$p1."/all");
            }
        }

        if ($p2 != '') {
            drupal_goto("mailcontent/content/".$p2."/all/".$p3);
        }

        $text = $text."<h2>".t('All received messages for Drupal')."</h2>";
        $text = $text.mailcontent_list_mail_all(0, 0, $p3);
        $text = $text."<hr><br><br>";
    } else {
        $text = "<hr><br><br>".t("You have not permission for this action!");
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $text;
} //OK

/*
 * Zobrazí anketu a dovolí v ní hlasovat
 * $idm - číslo článku, kde je anketa
 * @ vrací celou stránku
*/
function mailcontent_view_iquiry($idm = 0) {
    global $user;
    if (user_access('content edit')) {

        $text = "<hr><br><br>";
        $text = $text."<h2>".mailcontent_get_title($idm)."</h2>";
        $GLOBALS['mailcontent_idm'] = $idm;
        $text = $text.drupal_get_form(mailcontent_iquiry_form);
        $GLOBALS['mailcontent_idm'] = '';
    } else {
        $text = "<hr><br><br>".t("You have not permission for this action!");
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $text;
} //OK

/*
 * Vypisuje všechny přijaté maily typu article
 * @ vrací celou stránku výpisu
*/
function mailcontent_content_article($start = 0) {
    global $user;
    if (user_access('content user')) {

        $text = "<hr><br><br>";
        $text = $text."<h2>".t('All received messages for Drupal marked as article')."</h2>";
        $text = $text.mailcontent_list_article_all(0, 0, $start);
        $text = $text."<hr><br><br>";
    } else {
        $text = "<hr><br><br>".t("You have not permission for this action!");
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $text;
}

/*
 * Vypisuje všechny přijaté maily typu inquiry
 * @ vrací celou stránku výpisu
*/
function mailcontent_content_inquiry($start = 0) {
    global $user;
    if (user_access('content user')) {

        $text = "<hr><br><br>";

        $text = $text."<h2>".t('All received messages for Drupal marked as inquiry')."</h2>";
        $text = $text.mailcontent_list_inquiry_all(0,0,$start);
        $text = $text."<hr><br><br>";
    } else {
        $text = "<hr><br><br>".t("You have not permission for this action!");
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }

    return $text;
} //OK

/*
 * Vypisuje všechny přijaté maily typu schedule
 * @ vrací celou stránku výpisu
*/
function mailcontent_content_schedule($start = 0) {
    global $user;
    if (user_access('content user')) {


        $text = "<hr><br><br>";

        $text = $text."<h2>".t('All received messages for Drupal marked as schedule')."</h2>";
        $text = $text.mailcontent_list_schedule_all(0,0,$start);
        $text = $text."<hr><br><br>";

    } else {
        $text = "<hr><br><br>".t("You have not permission for this action!");
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $text;
} //OK

/*
 * Vypisuje všechny přijaté maily typu task
 * @ vrací celou stránku výpisu
*/
function mailcontent_content_task($start = 0) {
    global $user;
    if (user_access('content user')) {

        $text = "<hr><br><br>";
        $text = $text."<h2>".t('All received messages for Drupal marked as task')."</h2>";
        $text = $text.mailcontent_list_task_all(0, 0, $start);
        $text = $text."<hr><br><br>";

    } else {
        $text = "<hr><br><br>".t("You have not permission for this action!");
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
    return $text;
} //OK

/*
 *
 * Přesměrování na stránku contentu, kde byl uživatel pře provedenou akcí
 *
*/
function mailcontent_goto_content() {
    if ($GLOBALS['mailcontent_ret'] != '') {
        drupal_goto('mailcontent/content/'.$GLOBALS['mailcontent_ret']."/all");
    } else {
        drupal_goto('mailcontent/content');
    }
} //OK

/*
 * Zpracovává požadavek na přidání uživatele k nějaké akci nebo úkolu
 * $idm - ID mailu, kde je akce
*/
function  mailcontent_work_on($idm) {
    global $user;
    if (user_access('content user')) {

        global $user;
        if (db_result(db_query("SELECT * FROM {mailcontent_coworkers} WHERE idm=".$idm." AND duid=".$user->uid))) {
            if (db_query("DELETE FROM {mailcontent_coworkers} WHERE idm=".$idm." AND duid=".$user->uid)) {
                mailcontent_info(t("Suceffuly remove from database"));
            } else {
                mailcontent_error(t("Can not remove record from database"));
            }
        } else {
            $data['duid'] = $user->uid;
            $data['idm'] = $idm;
            if (drupal_write_record('mailcontent_coworkers', $data)) {
                mailcontent_info(t("Suceffuly added to database"));
            } else {
                mailcontent_error(t("Can not added record to database"));
            }
        }

    } else {
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
} //OK

/*
 * Přesměrování na zobrazení článku
 * $idn - ID článku
*/
function mailcontent_gotonode($idn) {
    drupal_goto('node/'.$idn);
} //OK

/*
 * Zobrazí mail, tak, jak přišel od uživatele - včetně všech klíčových slov
 * $idm - ID mailů, který se má zobrazit
 * @ vrací celou stránku výpisu
*/
function mailcontent_view_mail($idm = '') {

    global $user;
    if (user_access('content user')) {


        if ($idm == '') {
            return t('Mail can not be displayed');
        } else {
            $result = db_query("SELECT * FROM {mailcontent_mail} WHERE idm=".$idm);
            if ($result) {
                while ($row = db_fetch_array($result)) {
                    $page = "<br><br><br>";
                    $page = $page.t('From: ').$row['from_address'];
                    $page = $page."<br>";
                    $page = $page.t('Received: ').$row['received'];
                    $page = $page."<br>";
                    $page = $page.t('For: ').$row['to_address'];
                    $page = $page."<br><br>";
                    $page = $page.t('Subject + body: ')."<br>".$row['body'];
                    return $page;
                }
            } else {
                return t("Mail can not be displayed");
            }
        }
    } else {
        $text = "<hr><br><br>".t("You have not permission for this action!");
        global $user;
        mailcontent_error_permissions("User ".$user->uid." has not permission");
    }
} //OK
?>
